#include "parse.h"


bool read_weight_file(const char *filename){
        
        int instances, nets, inputs, outputs;
	const int MAX_LINE_LENGTH  = 65536;
        
        //cout << "Reading file..." << endl;
        char line_buffer[MAX_LINE_LENGTH];
 
        int line_num = 0;
        int index = 0;
        
        ifstream inp(filename);
        if (!inp.is_open()) {
                cerr << "Can't open input file" << endl;
                exit(1);
        }
        int gate_index = 0;
        
        while(inp.getline(line_buffer, MAX_LINE_LENGTH)){
                if(strcmp(line_buffer,"0"))
                {
                        while(C->getGate(gate_index)->IsCritical == false)
                                gate_index++;
                        
                        cout << "reading weight file..." << endl;
                        cout << C->getGate(gate_index)->getOutput(0)->getName() << endl;
                        C->getGate(gate_index++)->weight = atof(line_buffer);
                        //assert(C->getGate(gate_index++)->weight < 1.0);
                        //assert(C->getGate(gate_index++)->weight >= 0.0);
                }
        }
        
        cout << "gate index is" << gate_index << endl;
        cout << "gate size is" << C->gateSize() << endl;
        
        ofstream out("ck");
        for(gate_index = 0; gate_index < C->gateSize(); gate_index++)
                if(C->getGate(gate_index)->outputSize() != 0)
                        out << C->getGate(gate_index)->getOutput(0)->getName() << "\t" << C->getGate(gate_index)->weight << endl;
                else
                        out << C->getGate(gate_index)->weight << endl;
        
        out.close();
        return true;
}


bool parse(char *filename){

	int instances, nets, inputs, outputs;
	const int MAX_LINE_LENGTH = 65536;
        
        //cout << "Reading file..." << endl;
        char line_buffer[MAX_LINE_LENGTH];
        char line_buffer2[MAX_LINE_LENGTH];
 
        int line_num = 0;
        int index = 0;
  
        ifstream inp(filename);
        if (!inp.is_open()) {
                cerr << "Can't open bench file" << endl;
                exit(1);
        }
	/*
        string str = filename;
        str.erase(str.begin() + str.find("bench"), str.end());
        str += "dat";
        cout << "weight file name is " << str << endl;
        
        ifstream in(str.c_str());
        if (!in.is_open()) {
                cerr << "Can't open weight file" << endl;
                exit(1);
        }
	*/
	inp.getline(line_buffer, MAX_LINE_LENGTH);
	//cout<<line_buffer<<endl;
	++line_num;
	int arg = sscanf(line_buffer, "## No of instances %d", &instances);
	inp.getline(line_buffer, MAX_LINE_LENGTH);
	//cout<<line_buffer<<endl;
	++line_num;
	arg = sscanf(line_buffer, "## No of Nets %d", &nets);
	inp.getline(line_buffer, MAX_LINE_LENGTH);
	//cout<<line_buffer<<endl;
	++line_num;
	arg = sscanf(line_buffer, "## No of Inputs %d", &inputs);
	inp.getline(line_buffer, MAX_LINE_LENGTH);
	//cout<<line_buffer<<endl;
	++line_num;
	arg = sscanf(line_buffer, "## No of Outputs %d", &outputs);
	
	inp.getline(line_buffer, MAX_LINE_LENGTH);
	for(int i=0; i<3; ++i){
		inp.getline(line_buffer, MAX_LINE_LENGTH);
		++line_num;
	}
	//cout<<instances<<','<<nets<<','<<inputs<<','<<outputs<<endl;
	////cin.get();
	C = new Circuit();
	///inputs
	for(int i=0; i<inputs; ++i){
		string tmp;
		inp.getline(line_buffer, MAX_LINE_LENGTH);
                //	//cout<<i<<':'<<line_buffer<<endl;
		++line_num;
		int cnt=0;
		while(line_buffer[cnt++]!='(') {}
		while(line_buffer[cnt]!=')')
			tmp += line_buffer[cnt++];
		Signal *s=C->addSignal(tmp);
		InputNode *n = new InputNode();
		n->setIndex(index++);
		s->addOutput(n);
		n->addInput(s);
		C->addInput(n);	
	}

        //Add two more inputs, constant 0 and constant 1, these are used to 
        //fix the benchmark issue (missing inputs to the gates) Min 07/26/2013
        string str = "CONST_0";
        Signal *s=C->addSignal(str);
        InputNode *n = new InputNode();
        n->setIndex(index++);
        s->addOutput(n);
        n->addInput(s);
        C->addInput(n);	

        str = "CONST_1";
        Signal *s1=C->addSignal(str);
        InputNode *n1 = new InputNode();
        n1->setIndex(index++);
        s1->addOutput(n1);
        n1->addInput(s1);
        C->addInput(n1);
 
	/*
          string tmp = " ";
          Signal *s=C->addSignal(tmp);
          InputNode *n = new InputNode();
          n->setIndex(index++);
          s->addOutput(n);
          n->addInput(s);
          C->addInput(n);	
          inputs++;
	*/
	///outputs
	for(int i=0; i<outputs; ++i){
		string tmp;
		inp.getline(line_buffer, MAX_LINE_LENGTH);
                //	//cout<<i<<':'<<line_buffer<<endl;
		++line_num;
		int cnt=0;
		while(line_buffer[cnt++]!='('){}
		while(line_buffer[cnt]!=')')
			tmp += line_buffer[cnt++];
                Signal *s=C->addSignal(tmp);
		OutputNode *n = new OutputNode();
		n->setIndex(index++);
		n->addOutput(s);
		C->addOutput(n);		
		s->addInput(n);
	}
	
	for(int i=0; i<2; ++i){
		inp.getline(line_buffer, MAX_LINE_LENGTH);
                //	//cout<<i<<':'<<line_buffer<<endl;
		++line_num;
	}	
	///instances
	for(int i=0; i<instances; ++i){
                
                inp.getline(line_buffer, MAX_LINE_LENGTH);
		//in.getline(line_buffer2, MAX_LINE_LENGTH);
                //cout<<i<<':'<<line_buffer<<endl;
                ////cin.get();
		++line_num;
		if(line_buffer[0]=='('){ //flipflop
			FlipflopNode *n = new FlipflopNode();
			n->setIndex(index++);
			C->addFlipflop(n);
			int cnt=1;
			string tmp;
			while(line_buffer[cnt]!=',')
				tmp += line_buffer[cnt++];
			if(tmp[0] != ' '){
                                Signal *s=C->addSignal(tmp);	
				s->addInput(n);
				n->addOutput(s);
			}
			else{
				tmp = "s";
				char buffer [33];
				sprintf (buffer, "%i", C->signalSize());
				tmp += buffer;
				Signal *s=C->addSignal(tmp);
				s->addInput(n);
				n->addOutput(s);
			}
			tmp = "";
			while(line_buffer[++cnt]==' '){}
			while(line_buffer[cnt]!=')')
				tmp += line_buffer[cnt++];
                        
			if(tmp.size()){
				Signal *s=C->addSignal(tmp);
				NotNode *n1= new NotNode();
				n->setIndex(index++);
				C->addGate(n1);
				n1->addOutput(s);
				n1->addInput(n->getOutput(0));
				s->addInput(n1);
				n->getOutput(0)->addOutput(n1);
				n1->IsCritical = false;
			}
			while(line_buffer[cnt++]!='('){}
			tmp="";
			while(line_buffer[cnt]!=',')
				tmp += line_buffer[cnt++];
			Signal *s=C->addSignal(tmp);
			n->addInput(s);
			s->addOutput(n);
			
			tmp="";
			cnt++;
			cnt++;
			while(line_buffer[cnt]!=',')
				tmp += line_buffer[cnt++];
			if(tmp[0]!=' '){
				s=C->addSignal(tmp);
				n->addInput(s);
				s->addOutput(n);
			}
			else{
				tmp = "s";
				char buffer [33];
				sprintf (buffer, "%i", C->signalSize());
				tmp += buffer;
				Signal *s=C->addSignal(tmp);
				
				InputNode *n1=new InputNode();
				n1->setIndex(index++);
				C->addInput(n1);
				s->addOutput(n1);
				n1->addInput(s);
				s->addOutput(n);
				n->addInput(s);				
			}
			
			tmp="";
			cnt++;
			cnt++;
			while((line_buffer[cnt]!=')')&&(line_buffer[cnt]!=','))
				tmp += line_buffer[cnt++];				
			s=C->addSignal(tmp);
			n->addInput(s);
			s->addOutput(n);	
			
                }

                else{ //other gates
                        string tmp;
                        int cnt=0;
                        while(line_buffer[cnt]!=' ')
                                tmp += line_buffer[cnt++];
                        Signal *s=C->addSignal(tmp);
                        cnt += 3;
                        tmp="";
                        for(int i=0; i<5; ++i,cnt++)
                                tmp += line_buffer[cnt];
                        //	//cout<<tmp<<endl;
			if(tmp == "INV_X"){
				NotNode *n= new NotNode();
				n->setIndex(index++);
				C->addGate(n);
				n->addOutput(s);
				s->addInput(n);
				while(line_buffer[cnt++]!='('){}
				tmp="";
				while(line_buffer[cnt]!=')')
					tmp += line_buffer[cnt++];
				s=C->addSignal(tmp);
				n->addInput(s);
				s->addOutput(n);
                                //n->IsCritical = true;
                                n->setIsCrit( true );
                                //n->weight = atof(line_buffer2);
			}
			else if(tmp == "NAND2"){
				Nand2Node *n=new Nand2Node();
				n->setIndex(index++);
				C->addGate(n);
				n->addOutput(s);
				s->addInput(n);
				tmp="";
				while(line_buffer[cnt++]!='('){}
				while(line_buffer[cnt]!=',')
                                        tmp += line_buffer[cnt++];
                                
                                if(tmp[0] == ' ')//modified 07/26/2013
                                        tmp = "CONST_1";
                                
                                s=C->addSignal(tmp);
                                n->addInput(s);
                                s->addOutput(n);
                                
                                tmp="";
				cnt++;
				cnt++;
				while(line_buffer[cnt]!=')')
					tmp += line_buffer[cnt++];
				s=C->addSignal(tmp);
				n->addInput(s);
				s->addOutput(n);
                                //n->IsCritical = true;
                                n->setIsCrit( true );
                                //n->weight = atof(line_buffer2);
			}
			else if(tmp == "NOR2_" ){
				Nor2Node *n=new Nor2Node();
				n->setIndex(index++);
				C->addGate(n);
				n->addOutput(s);
				s->addInput(n);
				tmp="";
				while(line_buffer[cnt++]!='('){}
				while(line_buffer[cnt]!=',')
                                        tmp += line_buffer[cnt++];
				s=C->addSignal(tmp);
				n->addInput(s);
				s->addOutput(n);
                                
				tmp="";
				cnt++;
				cnt++;
				while(line_buffer[cnt]!=')')
					tmp += line_buffer[cnt++];
				s=C->addSignal(tmp);
				n->addInput(s);
				s->addOutput(n);
                                //n->IsCritical = true;
                                n->setIsCrit( true );
                                //n->weight = atof(line_buffer2);
                        }
			else if(tmp=="NAND3"){
                                Nand2Node *n1=new Nand2Node();
                                n1->setIndex(index++);
                                NotNode *n2 = new NotNode();
                                n2->setIndex(index++);
                                Nand2Node *n3=new Nand2Node();
                                n3->setIndex(index++);
                                C->addGate(n1);
                                C->addGate(n2);
                                C->addGate(n3);
                                n3->addOutput(s);
                                s->addInput(n3);
                                n3->setIsCrit( true );
                                //n3->weight = atof(line_buffer2);
                                
                                tmp = "s";
                                char buffer [33];
                                sprintf (buffer, "%i", C->signalSize());
                                tmp += buffer;
                                Signal *s1=C->addSignal(tmp);
                                n2->addInput(s1);
                                s1->addOutput(n2);
                                s1->addInput(n1);
                                n1->addOutput(s1);

                                tmp = "s";
                                sprintf (buffer, "%i", C->signalSize());
                                tmp += buffer;
                                Signal *s2=C->addSignal(tmp);
                                n2->addOutput(s2);
                                s2->addInput(n2);
                                s2->addOutput(n3);
                                n3->addInput(s2);

                                while(line_buffer[cnt++]!='('){}
                                tmp="";
                                while(line_buffer[cnt]!=',')
                                        tmp += line_buffer[cnt++];
                                
                                if(tmp[0] == ' ')//modified 07/26/2013
                                        tmp = "CONST_1";
                                
                                Signal *s3=C->addSignal(tmp);
                                n1->addInput(s3);
                                s3->addOutput(n1);
                                
                                tmp="";
                                cnt++;
                                cnt++;
                                while(line_buffer[cnt]!=',')
                                        tmp += line_buffer[cnt++];
                                
                                if(tmp[0] == ' ')//modified 07/26/2013
                                        tmp = "CONST_1";
                                
                                Signal *s4=C->addSignal(tmp);
                                n1->addInput(s4);
                                s4->addOutput(n1);

                                tmp="";
                                cnt++;
                                cnt++;
                                while(line_buffer[cnt]!=')')
                                        tmp += line_buffer[cnt++];
                                Signal *s5=C->addSignal(tmp);
                                n3->addInput(s5);
                                s5->addOutput(n3);
			}
			else if(tmp=="NOR3_"){
                                Nor2Node *n1=new Nor2Node();
                                n1->setIndex(index++);
                                NotNode *n2 = new NotNode();
                                n2->setIndex(index++);
                                Nor2Node *n3=new Nor2Node();
                                n3->setIndex(index++);
                                C->addGate(n1);
                                C->addGate(n2);
                                C->addGate(n3);
                                n3->addOutput(s);
                                s->addInput(n3);
                                n3->setIsCrit( true );
                                //n3->weight = atof(line_buffer2);

                                tmp = "s";
                                char buffer [33];
                                sprintf (buffer, "%i", C->signalSize());
                                tmp += buffer;
                                Signal *s1=C->addSignal(tmp);
                                n2->addInput(s1);
                                s1->addOutput(n2);
                                s1->addInput(n1);
                                n1->addOutput(s1);

                                tmp = "s";
                                sprintf (buffer, "%i", C->signalSize());
                                tmp += buffer;
                                Signal *s2=C->addSignal(tmp);
                                n2->addOutput(s2);
                                s2->addInput(n2);
                                s2->addOutput(n3);
                                n3->addInput(s2);

                                while(line_buffer[cnt++]!='('){}
                                tmp="";
                                while(line_buffer[cnt]!=',')
                                        tmp += line_buffer[cnt++];
                                Signal *s3=C->addSignal(tmp);
                                n1->addInput(s3);
                                s3->addOutput(n1);

                                tmp="";
                                cnt++;
                                cnt++;
                                while(line_buffer[cnt]!=',')
                                        tmp += line_buffer[cnt++];
                                Signal *s4=C->addSignal(tmp);
                                n1->addInput(s4);
                                s4->addOutput(n1);

                                tmp="";
                                cnt++;
                                cnt++;
                                while(line_buffer[cnt]!=')')
                                        tmp += line_buffer[cnt++];
                                Signal *s5=C->addSignal(tmp);
                                n3->addInput(s5);
                                s5->addOutput(n3);

			}
                        //------------------new gates start from here-------------------
                        //FA, HA, BUF, CLKGATE, MUX not considered here
                        else if(tmp=="AND2_"){
				Nand2Node *n1=new Nand2Node();
				n1->setIndex(index++);
				NotNode *n2 = new NotNode();
				n2->setIndex(index++);
				C->addGate(n1);
				C->addGate(n2);
                                n2->addOutput(s);
				s->addInput(n2);
                                n2->setIsCrit( true );
				//n2->weight = atof(line_buffer2);
                                
				tmp = "s";
				char buffer [33];
				sprintf (buffer, "%i", C->signalSize());
				tmp += buffer;
                                //cout<<tmp<<endl;
				Signal *s1=C->addSignal(tmp);
				n2->addInput(s1);
				s1->addOutput(n2);
				s1->addInput(n1);
				n1->addOutput(s1);
				
				while(line_buffer[cnt++]!='('){}
				tmp="";
				while(line_buffer[cnt]!=',')
					tmp += line_buffer[cnt++];
                                //cout<<tmp<<endl;
				Signal *s2=C->addSignal(tmp);
				n1->addInput(s2);
				s2->addOutput(n1);
				
				tmp="";
				cnt++;
				cnt++;
				while(line_buffer[cnt]!=')')
					tmp += line_buffer[cnt++];
                                //cout<<tmp<<endl;
				Signal *s3=C->addSignal(tmp);
				n1->addInput(s3);
				s3->addOutput(n1);
                                
                                //cin.get();
			}
                        else if(tmp=="AND3_"){
				Nand2Node *n1=new Nand2Node();
				n1->setIndex(index++);
				NotNode *n2 = new NotNode();
				n2->setIndex(index++);
				Nor2Node *n3=new Nor2Node();
				n3->setIndex(index++);
				C->addGate(n1);
				C->addGate(n2);
				C->addGate(n3);
				n3->addOutput(s);
				s->addInput(n3);
                                n3->setIsCrit( true );
                                //n3->weight = atof(line_buffer2);
                                
				tmp = "s";
				char buffer [33];
				sprintf (buffer, "%i", C->signalSize());
				tmp += buffer;
                                //cout<<tmp<<endl;
				Signal *s1=C->addSignal(tmp);
				n3->addInput(s1);
				s1->addOutput(n3);
				s1->addInput(n1);
				n1->addOutput(s1);
				
                                tmp = "s";
                                sprintf (buffer, "%i", C->signalSize());
				tmp += buffer;
                                //cout<<tmp<<endl;
				Signal *s2=C->addSignal(tmp);
                                n3->addInput(s2);
				s2->addOutput(n3);
				s2->addInput(n2);
				n2->addOutput(s2);
				
				while(line_buffer[cnt++]!='('){}
				tmp="";
				while(line_buffer[cnt]!=',')
					tmp += line_buffer[cnt++];
                                //cout<<tmp<<endl;
				Signal *s3=C->addSignal(tmp);
				n1->addInput(s3);
				s3->addOutput(n1);
				
				tmp="";
				cnt++;
				cnt++;
				while(line_buffer[cnt]!=',')
					tmp += line_buffer[cnt++];
                                //cout<<tmp<<endl;
				Signal *s4=C->addSignal(tmp);
				n1->addInput(s4);
				s4->addOutput(n1);
				
                                tmp="";
				cnt++;
				cnt++;
				while(line_buffer[cnt]!=')')
					tmp += line_buffer[cnt++];
                                //cout<<tmp<<endl;
				Signal *s5=C->addSignal(tmp);
				n2->addInput(s5);
				s5->addOutput(n2);
				
                                //cin.get();
		
			}
			else if(tmp=="AND4_"){
                                Nand2Node *n1=new Nand2Node();
				n1->setIndex(index++);
				Nand2Node *n2 = new Nand2Node();
				n2->setIndex(index++);
				Nor2Node *n3=new Nor2Node();
				n3->setIndex(index++);
				C->addGate(n1);
				C->addGate(n2);
				C->addGate(n3);
				n3->addOutput(s);
				s->addInput(n3);
                                n3->setIsCrit( true );
                                //n3->weight = atof(line_buffer2);

				tmp = "s";
				char buffer [33];
				sprintf (buffer, "%i", C->signalSize());
				tmp += buffer;
                                //cout<<tmp<<endl;
				Signal *s1=C->addSignal(tmp);
				n3->addInput(s1);
				s1->addOutput(n3);
				s1->addInput(n1);
				n1->addOutput(s1);
				
                                tmp = "s";
                                sprintf (buffer, "%i", C->signalSize());
				tmp += buffer;
                                //cout<<tmp<<endl;
				Signal *s2=C->addSignal(tmp);
                                n3->addInput(s2);
				s2->addOutput(n3);
				s2->addInput(n2);
				n2->addOutput(s2);
				
				while(line_buffer[cnt++]!='('){}
				tmp="";
				while(line_buffer[cnt]!=',')
					tmp += line_buffer[cnt++];
                                //cout<<tmp<<endl;
				Signal *s3=C->addSignal(tmp);
				n1->addInput(s3);
				s3->addOutput(n1);
				
				tmp="";
				cnt++;
				cnt++;
				while(line_buffer[cnt]!=',')
					tmp += line_buffer[cnt++];
                                //cout<<tmp<<endl;
				Signal *s4=C->addSignal(tmp);
				n1->addInput(s4);
				s4->addOutput(n1);
				
                                tmp="";
				cnt++;
				cnt++;
				while(line_buffer[cnt]!=',')
					tmp += line_buffer[cnt++];
                                //cout<<tmp<<endl;
				Signal *s5=C->addSignal(tmp);
				n2->addInput(s5);
				s5->addOutput(n2);
				
				tmp="";
				cnt++;
				cnt++;
				while(line_buffer[cnt]!=')')
					tmp += line_buffer[cnt++];
                                //cout<<tmp<<endl;
				Signal *s6=C->addSignal(tmp);
				n2->addInput(s6);
				s6->addOutput(n2);
                                
                                //cin.get();
                                
			}
			else if(tmp=="AOI21"){//AOI21 && AOI211 
                                
                                //cnt++;
                                //cout << "line_buffer[cnt] is " << line_buffer[cnt] <<endl;
                                if(line_buffer[cnt]=='1')//AOI211
                                {
                                        Nand2Node *n1=new Nand2Node();
                                        n1->setIndex(index++);
                                        Nor2Node *n2 = new Nor2Node();
                                        n2->setIndex(index++);
                                        Nand2Node *n3=new Nand2Node();
                                        n3->setIndex(index++);
                                        NotNode *n4=new NotNode();
                                        n4->setIndex(index++);
                                        C->addGate(n1);
                                        C->addGate(n2);
                                        C->addGate(n3);
                                        C->addGate(n4);
                                        n4->addOutput(s);
                                        s->addInput(n4);
                                        n4->setIsCrit( true );
                                        //n4->weight = atof(line_buffer2);
                                        
                                        tmp = "s";
                                        char buffer [33];
                                        sprintf (buffer, "%i", C->signalSize());
                                        tmp += buffer;
                                        //cout<<tmp<<endl;
                                        Signal *s1=C->addSignal(tmp);
                                        n3->addInput(s1);
                                        s1->addOutput(n3);
                                        s1->addInput(n1);
                                        n1->addOutput(s1);
				
                                        tmp = "s";
                                        sprintf (buffer, "%i", C->signalSize());
                                        tmp += buffer;
                                        //cout<<tmp<<endl;
                                        Signal *s2=C->addSignal(tmp);
                                        n2->addOutput(s2);
                                        s2->addInput(n2);
                                        s2->addOutput(n3);
                                        n3->addInput(s2);
				
                                        tmp = "s";
                                        sprintf (buffer, "%i", C->signalSize());
                                        tmp += buffer;
                                        //cout<<tmp<<endl;
                                        Signal *s3=C->addSignal(tmp);
                                        n3->addOutput(s3);
                                        s3->addInput(n3);
                                        s3->addOutput(n4);
                                        n4->addInput(s3);
				
                                        while(line_buffer[cnt++]!='('){}
                                        tmp="";
                                        while(line_buffer[cnt]!=',')
                                                tmp += line_buffer[cnt++];
                                        //cout<<tmp<<endl;
                                        Signal *s4=C->addSignal(tmp);
                                        n1->addInput(s4);
                                        s4->addOutput(n1);
				
                                        tmp="";
                                        cnt++;
                                        cnt++;
                                        while(line_buffer[cnt]!=',')
                                                tmp += line_buffer[cnt++];
                                        //cout<<tmp<<endl;
                                        Signal *s5=C->addSignal(tmp);
                                        n1->addInput(s5);
                                        s5->addOutput(n1);
                                        
                                        tmp="";
                                        cnt++;
                                        cnt++;
                                        while(line_buffer[cnt]!=',')
                                                tmp += line_buffer[cnt++];
                                        //cout<<tmp<<endl;
                                        Signal *s6=C->addSignal(tmp);
                                        n2->addInput(s6);
                                        s6->addOutput(n2);
				
                                        tmp="";
                                        cnt++;
                                        cnt++;
                                        while(line_buffer[cnt]!=')')
                                                tmp += line_buffer[cnt++];
                                        //cout<<tmp<<endl;
                                        Signal *s7=C->addSignal(tmp);
                                        n2->addInput(s7);
                                        s7->addOutput(n2);
                                        
                                        //cin.get();

                                }
                                else//AOI21
                                {
                                        Nand2Node *n1=new Nand2Node();
                                        n1->setIndex(index++);
                                        NotNode *n2 = new NotNode();
                                        n2->setIndex(index++);
                                        Nand2Node *n3=new Nand2Node();
                                        n3->setIndex(index++);
                                        NotNode *n4=new NotNode();
                                        n4->setIndex(index++);
                                        C->addGate(n1);
                                        C->addGate(n2);
                                        C->addGate(n3);
                                        C->addGate(n4);
                                        n4->addOutput(s);
                                        s->addInput(n4);
                                        n4->setIsCrit( true );
                                        //n4->weight = atof(line_buffer2);
                                        
                                        tmp = "s";
                                        char buffer [33];
                                        sprintf (buffer, "%i", C->signalSize());
                                        tmp += buffer;
                                        //cout<<tmp<<endl;
                                        Signal *s1=C->addSignal(tmp);
                                        n3->addInput(s1);
                                        s1->addOutput(n3);
                                        s1->addInput(n1);
                                        n1->addOutput(s1);
				
                                        tmp = "s";
                                        sprintf (buffer, "%i", C->signalSize());
                                        tmp += buffer;
                                        //cout<<tmp<<endl;
                                        Signal *s2=C->addSignal(tmp);
                                        n2->addOutput(s2);
                                        s2->addInput(n2);
                                        s2->addOutput(n3);
                                        n3->addInput(s2);
				
                                        tmp = "s";
                                        sprintf (buffer, "%i", C->signalSize());
                                        tmp += buffer;
                                        //cout<<tmp<<endl;
                                        Signal *s3=C->addSignal(tmp);
                                        n3->addOutput(s3);
                                        s3->addInput(n3);
                                        s3->addOutput(n4);
                                        n4->addInput(s3);
				
                                        while(line_buffer[cnt++]!='('){}
                                        tmp="";
                                        while(line_buffer[cnt]!=',')
                                                tmp += line_buffer[cnt++];
                                        //cout<<tmp<<endl;
                                        Signal *s4=C->addSignal(tmp);
                                        n1->addInput(s4);
                                        s4->addOutput(n1);
				
                                        tmp="";
                                        cnt++;
                                        cnt++;
                                        while(line_buffer[cnt]!=',')
                                                tmp += line_buffer[cnt++];
                                        //cout<<tmp<<endl;
                                        Signal *s5=C->addSignal(tmp);
                                        n1->addInput(s5);
                                        s5->addOutput(n1);
                                        
                                        tmp="";
                                        cnt++;
                                        cnt++;
                                        while(line_buffer[cnt]!=')')
                                                tmp += line_buffer[cnt++];
                                        //cout<<tmp<<endl;
                                        Signal *s6=C->addSignal(tmp);
                                        n2->addInput(s6);
                                        s6->addOutput(n2);
                                        
                                        //cin.get();
                                        
                                }
			}
                        else if(tmp=="AOI22"){//AOI221 && AOI222 && AOI22 
                                
                                //cnt++;
                                if(line_buffer[cnt]=='1')//AOI221
                                {
                                        Nand2Node *n1=new Nand2Node();
                                        n1->setIndex(index++);
                                        Nand2Node *n2 = new Nand2Node();
                                        n2->setIndex(index++);
                                        Nand2Node *n3=new Nand2Node();
                                        n3->setIndex(index++);
                                        Nor2Node *n4=new Nor2Node();
                                        n4->setIndex(index++);
                                        C->addGate(n1);
                                        C->addGate(n2);
                                        C->addGate(n3);
                                        C->addGate(n4);
                                        n4->addOutput(s);
                                        s->addInput(n4);
                                        n4->setIsCrit( true );
                                        //n4->weight = atof(line_buffer2);

                                        tmp = "s";
                                        char buffer [33];
                                        sprintf (buffer, "%i", C->signalSize());
                                        tmp += buffer;
                                        //cout<<tmp<<endl;
                                        Signal *s1=C->addSignal(tmp);
                                        n3->addInput(s1);
                                        s1->addOutput(n3);
                                        s1->addInput(n1);
                                        n1->addOutput(s1);
				
                                        tmp = "s";
                                        sprintf (buffer, "%i", C->signalSize());
                                        tmp += buffer;
                                        //cout<<tmp<<endl;
                                        Signal *s2=C->addSignal(tmp);
                                        n2->addOutput(s2);
                                        s2->addInput(n2);
                                        s2->addOutput(n3);
                                        n3->addInput(s2);
				
                                        tmp = "s";
                                        sprintf (buffer, "%i", C->signalSize());
                                        tmp += buffer;
                                        //cout<<tmp<<endl;
                                        Signal *s3=C->addSignal(tmp);
                                        n3->addOutput(s3);
                                        s3->addInput(n3);
                                        s3->addOutput(n4);
                                        n4->addInput(s3);
				
                                        while(line_buffer[cnt++]!='('){}
                                        tmp="";
                                        while(line_buffer[cnt]!=',')
                                                tmp += line_buffer[cnt++];
                                        //cout<<tmp<<endl;
                                        Signal *s4=C->addSignal(tmp);
                                        n1->addInput(s4);
                                        s4->addOutput(n1);
				
                                        tmp="";
                                        cnt++;
                                        cnt++;
                                        while(line_buffer[cnt]!=',')
                                                tmp += line_buffer[cnt++];
                                        //cout<<tmp<<endl;
                                        Signal *s5=C->addSignal(tmp);
                                        n1->addInput(s5);
                                        s5->addOutput(n1);
                                        
                                        tmp="";
                                        cnt++;
                                        cnt++;
                                        while(line_buffer[cnt]!=',')
                                                tmp += line_buffer[cnt++];
                                        //cout<<tmp<<endl;
                                        Signal *s6=C->addSignal(tmp);
                                        n2->addInput(s6);
                                        s6->addOutput(n2);
				
                                        tmp="";
                                        cnt++;
                                        cnt++;
                                        while(line_buffer[cnt]!=',')
                                                tmp += line_buffer[cnt++];
                                        //cout<<tmp<<endl;
                                        Signal *s7=C->addSignal(tmp);
                                        n2->addInput(s7);
                                        s7->addOutput(n2);
                                        
                                        tmp="";
                                        cnt++;
                                        cnt++;
                                        while(line_buffer[cnt]!=')')
                                                tmp += line_buffer[cnt++];
                                        //cout<<tmp<<endl;
                                        Signal *s8=C->addSignal(tmp);
                                        n4->addInput(s8);
                                        s8->addOutput(n4);
                                        
                                        //cin.get();
                                        
                                }
                                else if(line_buffer[cnt]=='2')//AOI222
                                {
                                        Nand2Node *n1=new Nand2Node();
                                        n1->setIndex(index++);
                                        Nand2Node *n2 = new Nand2Node();
                                        n2->setIndex(index++);
                                        Nand2Node *n3=new Nand2Node();
                                        n3->setIndex(index++);
                                        Nand2Node *n4=new Nand2Node();
                                        n4->setIndex(index++);
                                        NotNode *n5=new NotNode();
                                        n5->setIndex(index++);
                                        Nor2Node *n6=new Nor2Node();
                                        n6->setIndex(index++);
                                        C->addGate(n1);
                                        C->addGate(n2);
                                        C->addGate(n3);
                                        C->addGate(n4);
                                        C->addGate(n5);
                                        C->addGate(n6);
                                        n6->addOutput(s);
                                        s->addInput(n6);
                                        n6->setIsCrit( true );
                                        //n6->weight = atof(line_buffer2);
                                        
                                        tmp = "s";
                                        char buffer [33];
                                        sprintf (buffer, "%i", C->signalSize());
                                        tmp += buffer;
                                        //cout<<tmp<<endl;
                                        Signal *s1=C->addSignal(tmp);
                                        n3->addInput(s1);
                                        s1->addOutput(n3);
                                        s1->addInput(n1);
                                        n1->addOutput(s1);
				
                                        tmp = "s";
                                        sprintf (buffer, "%i", C->signalSize());
                                        tmp += buffer;
                                        //cout<<tmp<<endl;
                                        Signal *s2=C->addSignal(tmp);
                                        n2->addOutput(s2);
                                        s2->addInput(n2);
                                        s2->addOutput(n3);
                                        n3->addInput(s2);
				
                                        tmp = "s";
                                        sprintf (buffer, "%i", C->signalSize());
                                        tmp += buffer;
                                        //cout<<tmp<<endl;
                                        Signal *s3=C->addSignal(tmp);
                                        n3->addOutput(s3);
                                        s3->addInput(n3);
                                        s3->addOutput(n6);
                                        n6->addInput(s3);
                                        
                                        tmp = "s";
                                        sprintf (buffer, "%i", C->signalSize());
                                        tmp += buffer;
                                        //cout<<tmp<<endl;
                                        Signal *s4=C->addSignal(tmp);
                                        n4->addOutput(s4);
                                        s4->addInput(n4);
                                        s4->addOutput(n5);
                                        n5->addInput(s4);
                                        
                                        tmp = "s";
                                        sprintf (buffer, "%i", C->signalSize());
                                        tmp += buffer;
                                        //cout<<tmp<<endl;
                                        Signal *s5=C->addSignal(tmp);
                                        n5->addOutput(s5);
                                        s5->addInput(n5);
                                        s5->addOutput(n6);
                                        n6->addInput(s5);
                                        
                                        while(line_buffer[cnt++]!='('){}
                                        tmp="";
                                        while(line_buffer[cnt]!=',')
                                                tmp += line_buffer[cnt++];
                                        //cout<<tmp<<endl;
                                        Signal *s6=C->addSignal(tmp);
                                        n1->addInput(s6);
                                        s6->addOutput(n1);
				
                                        tmp="";
                                        cnt++;
                                        cnt++;
                                        while(line_buffer[cnt]!=',')
                                                tmp += line_buffer[cnt++];
                                        //cout<<tmp<<endl;
                                        Signal *s7=C->addSignal(tmp);
                                        n1->addInput(s7);
                                        s7->addOutput(n1);
                                        
                                        tmp="";
                                        cnt++;
                                        cnt++;
                                        while(line_buffer[cnt]!=',')
                                                tmp += line_buffer[cnt++];
                                        //cout<<tmp<<endl;
                                        Signal *s8=C->addSignal(tmp);
                                        n2->addInput(s8);
                                        s8->addOutput(n2);
				
                                        tmp="";
                                        cnt++;
                                        cnt++;
                                        while(line_buffer[cnt]!=',')
                                                tmp += line_buffer[cnt++];
                                        //cout<<tmp<<endl;
                                        Signal *s9=C->addSignal(tmp);
                                        n2->addInput(s9);
                                        s9->addOutput(n2);
                                        
                                        tmp="";
                                        cnt++;
                                        cnt++;
                                        while(line_buffer[cnt]!=',')
                                                tmp += line_buffer[cnt++];
                                        //cout<<tmp<<endl;
                                        Signal *s10=C->addSignal(tmp);
                                        n4->addInput(s10);
                                        s10->addOutput(n4);
                                        
                                        tmp="";
                                        cnt++;
                                        cnt++;
                                        while(line_buffer[cnt]!=')')
                                                tmp += line_buffer[cnt++];
                                        //cout<<tmp<<endl;
                                        Signal *s11=C->addSignal(tmp);
                                        n4->addInput(s11);
                                        s11->addOutput(n4);
                                        
                                        //cin.get();
                                        
                                }
                                else//AOI22
                                {
                                        Nand2Node *n1=new Nand2Node();
                                        n1->setIndex(index++);
                                        Nand2Node *n2 = new Nand2Node();
                                        n2->setIndex(index++);
                                        Nand2Node *n3=new Nand2Node();
                                        n3->setIndex(index++);
                                        NotNode *n4=new NotNode();
                                        n4->setIndex(index++);
                                        C->addGate(n1);
                                        C->addGate(n2);
                                        C->addGate(n3);
                                        C->addGate(n4);
                                        n4->addOutput(s);
                                        s->addInput(n4);
                                        n4->setIsCrit( true );
                                        //n4->weight = atof(line_buffer2);
                                        
                                        tmp = "s";
                                        char buffer [33];
                                        sprintf (buffer, "%i", C->signalSize());
                                        tmp += buffer;
                                        //cout<<tmp<<endl;
                                        Signal *s1=C->addSignal(tmp);
                                        n3->addInput(s1);
                                        s1->addOutput(n3);
                                        s1->addInput(n1);
                                        n1->addOutput(s1);
				
                                        tmp = "s";
                                        sprintf (buffer, "%i", C->signalSize());
                                        tmp += buffer;
                                        //cout<<tmp<<endl;
                                        Signal *s2=C->addSignal(tmp);
                                        n2->addOutput(s2);
                                        s2->addInput(n2);
                                        s2->addOutput(n3);
                                        n3->addInput(s2);
				
                                        tmp = "s";
                                        sprintf (buffer, "%i", C->signalSize());
                                        tmp += buffer;
                                        //cout<<tmp<<endl;
                                        Signal *s3=C->addSignal(tmp);
                                        n3->addOutput(s3);
                                        s3->addInput(n3);
                                        s3->addOutput(n4);
                                        n4->addInput(s3);
                                        
                                        while(line_buffer[cnt++]!='('){}
                                        tmp="";
                                        while(line_buffer[cnt]!=',')
                                                tmp += line_buffer[cnt++];
                                        //cout<<tmp<<endl;
                                        Signal *s4=C->addSignal(tmp);
                                        n1->addInput(s4);
                                        s4->addOutput(n1);
				
                                        tmp="";
                                        cnt++;
                                        cnt++;
                                        while(line_buffer[cnt]!=',')
                                                tmp += line_buffer[cnt++];
                                        //cout<<tmp<<endl;
                                        Signal *s5=C->addSignal(tmp);
                                        n1->addInput(s5);
                                        s5->addOutput(n1);
                                        
                                        tmp="";
                                        cnt++;
                                        cnt++;
                                        while(line_buffer[cnt]!=',')
                                                tmp += line_buffer[cnt++];
                                        //cout<<tmp<<endl;
                                        Signal *s6=C->addSignal(tmp);
                                        n2->addInput(s6);
                                        s6->addOutput(n2);
                                        
                                        tmp="";
                                        cnt++;
                                        cnt++;
                                        while(line_buffer[cnt]!=')')
                                                tmp += line_buffer[cnt++];
                                        //cout<<tmp<<endl;
                                        Signal *s7=C->addSignal(tmp);
                                        n2->addInput(s7);
                                        s7->addOutput(n2);
                                        
                                        //cin.get();
                                }
			}
			else if(tmp=="NAND4"){
                                Nand2Node *n1=new Nand2Node();
				n1->setIndex(index++);
				Nand2Node *n2 = new Nand2Node();
				n2->setIndex(index++);
				Nor2Node *n3=new Nor2Node();
				n3->setIndex(index++);
                                NotNode *n4=new NotNode();
				n4->setIndex(index++);
				C->addGate(n1);
				C->addGate(n2);
				C->addGate(n3);
                                C->addGate(n4);
				n4->addOutput(s);
				s->addInput(n4);
                                n4->setIsCrit( true );
                                //n4->weight = atof(line_buffer2);
                                
				tmp = "s";
				char buffer [33];
				sprintf (buffer, "%i", C->signalSize());
				tmp += buffer;
                                //cout<<tmp<<endl;
				Signal *s1=C->addSignal(tmp);
				n3->addInput(s1);
				s1->addOutput(n3);
				s1->addInput(n1);
				n1->addOutput(s1);
				
                                tmp = "s";
                                sprintf (buffer, "%i", C->signalSize());
				tmp += buffer;
                                //cout<<tmp<<endl;
				Signal *s2=C->addSignal(tmp);
                                n3->addInput(s2);
				s2->addOutput(n3);
				s2->addInput(n2);
				n2->addOutput(s2);
                                
                                tmp = "s";
                                sprintf (buffer, "%i", C->signalSize());
				tmp += buffer;
                                //cout<<tmp<<endl;
				Signal *s3=C->addSignal(tmp);
                                n4->addInput(s3);
				s3->addOutput(n4);
				s3->addInput(n3);
				n3->addOutput(s3);
				
				while(line_buffer[cnt++]!='('){}
				tmp="";
				while(line_buffer[cnt]!=',')
					tmp += line_buffer[cnt++];
                                //cout<<tmp<<endl;
				Signal *s4=C->addSignal(tmp);
				n1->addInput(s4);
				s4->addOutput(n1);
				
				tmp="";
				cnt++;
				cnt++;
				while(line_buffer[cnt]!=',')
					tmp += line_buffer[cnt++];
                                //cout<<tmp<<endl;
				Signal *s5=C->addSignal(tmp);
				n1->addInput(s5);
				s5->addOutput(n1);
				
                                tmp="";
				cnt++;
				cnt++;
				while(line_buffer[cnt]!=',')
					tmp += line_buffer[cnt++];
                                //cout<<tmp<<endl;
				Signal *s6=C->addSignal(tmp);
				n2->addInput(s6);
				s6->addOutput(n2);
				
				tmp="";
				cnt++;
				cnt++;
				while(line_buffer[cnt]!=')')
					tmp += line_buffer[cnt++];
                                //cout<<tmp<<endl;
				Signal *s7=C->addSignal(tmp);
				n2->addInput(s7);
				s7->addOutput(n2);
                                
                                //cin.get();
			}
			else if(tmp=="NOR4_"){
                                Nor2Node *n1=new Nor2Node();
				n1->setIndex(index++);
				Nor2Node *n2 = new Nor2Node();
				n2->setIndex(index++);
				Nand2Node *n3=new Nand2Node();
				n3->setIndex(index++);
                                NotNode *n4=new NotNode();
				n4->setIndex(index++);
				C->addGate(n1);
				C->addGate(n2);
				C->addGate(n3);
                                C->addGate(n4);
				n4->addOutput(s);
				s->addInput(n4);
                                n4->setIsCrit( true );
                                //n4->weight = atof(line_buffer2);
                                
				tmp = "s";
				char buffer [33];
				sprintf (buffer, "%i", C->signalSize());
				tmp += buffer;
                                //cout<<tmp<<endl;
				Signal *s1=C->addSignal(tmp);
				n3->addInput(s1);
				s1->addOutput(n3);
				s1->addInput(n1);
				n1->addOutput(s1);
				
                                tmp = "s";
                                sprintf (buffer, "%i", C->signalSize());
				tmp += buffer;
                                //cout<<tmp<<endl;
				Signal *s2=C->addSignal(tmp);
                                n3->addInput(s2);
				s2->addOutput(n3);
				s2->addInput(n2);
				n2->addOutput(s2);
                                
                                tmp = "s";
                                sprintf (buffer, "%i", C->signalSize());
				tmp += buffer;
                                //cout<<tmp<<endl;
				Signal *s3=C->addSignal(tmp);
                                n4->addInput(s3);
				s3->addOutput(n4);
				s3->addInput(n3);
				n3->addOutput(s3);
				
				while(line_buffer[cnt++]!='('){}
				tmp="";
				while(line_buffer[cnt]!=',')
					tmp += line_buffer[cnt++];
                                //cout<<tmp<<endl;
				Signal *s4=C->addSignal(tmp);
				n1->addInput(s4);
				s4->addOutput(n1);
				
				tmp="";
				cnt++;
				cnt++;
				while(line_buffer[cnt]!=',')
					tmp += line_buffer[cnt++];
                                //cout<<tmp<<endl;
				Signal *s5=C->addSignal(tmp);
				n1->addInput(s5);
				s5->addOutput(n1);
				
                                tmp="";
				cnt++;
				cnt++;
				while(line_buffer[cnt]!=',')
					tmp += line_buffer[cnt++];
                                //cout<<tmp<<endl;
				Signal *s6=C->addSignal(tmp);
				n2->addInput(s6);
				s6->addOutput(n2);
				
				tmp="";
				cnt++;
				cnt++;
				while(line_buffer[cnt]!=')')
					tmp += line_buffer[cnt++];
                                //cout<<tmp<<endl;
				Signal *s7=C->addSignal(tmp);
				n2->addInput(s7);
				s7->addOutput(n2);
                                
                                //cin.get();
			}
                        else if(tmp=="OAI21"){//OAI211 && OAI21
                                
                                //cnt++;
                                if(line_buffer[cnt]=='1')//OAI211
                                {
                                        Nor2Node *n1=new Nor2Node();
                                        n1->setIndex(index++);
                                        Nand2Node *n2 = new Nand2Node();
                                        n2->setIndex(index++);
                                        Nor2Node *n3=new Nor2Node();
                                        n3->setIndex(index++);
                                        NotNode *n4=new NotNode();
                                        n4->setIndex(index++);
                                        C->addGate(n1);
                                        C->addGate(n2);
                                        C->addGate(n3);
                                        C->addGate(n4);
                                        n4->addOutput(s);
                                        s->addInput(n4);
                                        n4->setIsCrit( true );
                                        //n4->weight = atof(line_buffer2);
                                        
                                        tmp = "s";
                                        char buffer [33];
                                        sprintf (buffer, "%i", C->signalSize());
                                        tmp += buffer;
                                        //cout<<tmp<<endl;
                                        Signal *s1=C->addSignal(tmp);
                                        n3->addInput(s1);
                                        s1->addOutput(n3);
                                        s1->addInput(n1);
                                        n1->addOutput(s1);
				
                                        tmp = "s";
                                        sprintf (buffer, "%i", C->signalSize());
                                        tmp += buffer;
                                        //cout<<tmp<<endl;
                                        Signal *s2=C->addSignal(tmp);
                                        n2->addOutput(s2);
                                        s2->addInput(n2);
                                        s2->addOutput(n3);
                                        n3->addInput(s2);
				
                                        tmp = "s";
                                        sprintf (buffer, "%i", C->signalSize());
                                        tmp += buffer;
                                        //cout<<tmp<<endl;
                                        Signal *s3=C->addSignal(tmp);
                                        n3->addOutput(s3);
                                        s3->addInput(n3);
                                        s3->addOutput(n4);
                                        n4->addInput(s3);
                                        
                                        while(line_buffer[cnt++]!='('){}
                                        tmp="";
                                        while(line_buffer[cnt]!=',')
                                                tmp += line_buffer[cnt++];
                                        //cout<<tmp<<endl;
                                        Signal *s4=C->addSignal(tmp);
                                        n1->addInput(s4);
                                        s4->addOutput(n1);
				
                                        tmp="";
                                        cnt++;
                                        cnt++;
                                        while(line_buffer[cnt]!=',')
                                                tmp += line_buffer[cnt++];
                                        //cout<<tmp<<endl;
                                        Signal *s5=C->addSignal(tmp);
                                        n1->addInput(s5);
                                        s5->addOutput(n1);
                                        
                                        tmp="";
                                        cnt++;
                                        cnt++;
                                        while(line_buffer[cnt]!=',')
                                                tmp += line_buffer[cnt++];
                                        //cout<<tmp<<endl;
                                        Signal *s6=C->addSignal(tmp);
                                        n2->addInput(s6);
                                        s6->addOutput(n2);
                                        
                                        tmp="";
                                        cnt++;
                                        cnt++;
                                        while(line_buffer[cnt]!=')')
                                                tmp += line_buffer[cnt++];
                                        //cout<<tmp<<endl;
                                        Signal *s7=C->addSignal(tmp);
                                        n2->addInput(s7);
                                        s7->addOutput(n2);
                                        
                                        //cin.get();
                                }
                                else//OAI21
                                {
                                        Nor2Node *n1=new Nor2Node();
                                        n1->setIndex(index++);
                                        NotNode *n2 = new NotNode();
                                        n2->setIndex(index++);
                                        Nor2Node *n3=new Nor2Node();
                                        n3->setIndex(index++);
                                        NotNode *n4=new NotNode();
                                        n4->setIndex(index++);
                                        C->addGate(n1);
                                        C->addGate(n2);
                                        C->addGate(n3);
                                        C->addGate(n4);
                                        n4->addOutput(s);
                                        s->addInput(n4);
                                        n4->setIsCrit( true );
                                        //n4->weight = atof(line_buffer2);
                                        
                                        tmp = "s";
                                        char buffer [33];
                                        sprintf (buffer, "%i", C->signalSize());
                                        tmp += buffer;
                                        //cout<<tmp<<endl;
                                        Signal *s1=C->addSignal(tmp);
                                        n3->addInput(s1);
                                        s1->addOutput(n3);
                                        s1->addInput(n1);
                                        n1->addOutput(s1);
				
                                        tmp = "s";
                                        sprintf (buffer, "%i", C->signalSize());
                                        tmp += buffer;
                                        //cout<<tmp<<endl;
                                        Signal *s2=C->addSignal(tmp);
                                        n2->addOutput(s2);
                                        s2->addInput(n2);
                                        s2->addOutput(n3);
                                        n3->addInput(s2);
				
                                        tmp = "s";
                                        sprintf (buffer, "%i", C->signalSize());
                                        tmp += buffer;
                                        //cout<<tmp<<endl;
                                        Signal *s3=C->addSignal(tmp);
                                        n3->addOutput(s3);
                                        s3->addInput(n3);
                                        s3->addOutput(n4);
                                        n4->addInput(s3);
                                        
                                        while(line_buffer[cnt++]!='('){}
                                        tmp="";
                                        while(line_buffer[cnt]!=',')
                                                tmp += line_buffer[cnt++];
                                        //cout<<tmp<<endl;
                                        Signal *s4=C->addSignal(tmp);
                                        n1->addInput(s4);
                                        s4->addOutput(n1);
				
                                        tmp="";
                                        cnt++;
                                        cnt++;
                                        while(line_buffer[cnt]!=',')
                                                tmp += line_buffer[cnt++];
                                        //cout<<tmp<<endl;
                                        Signal *s5=C->addSignal(tmp);
                                        n1->addInput(s5);
                                        s5->addOutput(n1);
                                        
                                        tmp="";
                                        cnt++;
                                        cnt++;
                                        while(line_buffer[cnt]!=')')
                                                tmp += line_buffer[cnt++];
                                        //cout<<tmp<<endl;
                                        Signal *s6=C->addSignal(tmp);
                                        n2->addInput(s6);
                                        s6->addOutput(n2);
                                        
                                        //cin.get();
                                }
                                
			}
                        else if(tmp=="OAI22"){//OAI221 && OAI22
                                
                                //cnt++;
                                if(line_buffer[cnt]=='1')//OAI221
                                {
                                        Nor2Node *n1=new Nor2Node();
                                        n1->setIndex(index++);
                                        Nor2Node *n2 = new Nor2Node();
                                        n2->setIndex(index++);
                                        Nor2Node *n3=new Nor2Node();
                                        n3->setIndex(index++);
                                        Nand2Node *n4=new Nand2Node();
                                        n4->setIndex(index++);
                                        C->addGate(n1);
                                        C->addGate(n2);
                                        C->addGate(n3);
                                        C->addGate(n4);
                                        n4->addOutput(s);
                                        s->addInput(n4);
                                        n4->setIsCrit( true );
                                        //n4->weight = atof(line_buffer2);
                                        
                                        tmp = "s";
                                        char buffer [33];
                                        sprintf (buffer, "%i", C->signalSize());
                                        tmp += buffer;
                                        Signal *s1=C->addSignal(tmp);
                                        n3->addInput(s1);
                                        s1->addOutput(n3);
                                        s1->addInput(n1);
                                        n1->addOutput(s1);
				
                                        tmp = "s";
                                        sprintf (buffer, "%i", C->signalSize());
                                        tmp += buffer;
                                        Signal *s2=C->addSignal(tmp);
                                        n2->addOutput(s2);
                                        s2->addInput(n2);
                                        s2->addOutput(n3);
                                        n3->addInput(s2);
				
                                        tmp = "s";
                                        sprintf (buffer, "%i", C->signalSize());
                                        tmp += buffer;
                                        Signal *s3=C->addSignal(tmp);
                                        n3->addOutput(s3);
                                        s3->addInput(n3);
                                        s3->addOutput(n4);
                                        n4->addInput(s3);
                                        
                                        while(line_buffer[cnt++]!='('){}
                                        tmp="";
                                        while(line_buffer[cnt]!=',')
                                                tmp += line_buffer[cnt++];
                                        //cout<<tmp<<endl;
                                        Signal *s4=C->addSignal(tmp);
                                        n1->addInput(s4);
                                        s4->addOutput(n1);
				
                                        tmp="";
                                        cnt++;
                                        cnt++;
                                        while(line_buffer[cnt]!=',')
                                                tmp += line_buffer[cnt++];
                                        //cout<<tmp<<endl;
                                        Signal *s5=C->addSignal(tmp);
                                        n1->addInput(s5);
                                        s5->addOutput(n1);
                                        
                                        tmp="";
                                        cnt++;
                                        cnt++;
                                        while(line_buffer[cnt]!=',')
                                                tmp += line_buffer[cnt++];
                                        //cout<<tmp<<endl;
                                        Signal *s6=C->addSignal(tmp);
                                        n2->addInput(s6);
                                        s6->addOutput(n2);
                                        
                                        tmp="";
                                        cnt++;
                                        cnt++;
                                        while(line_buffer[cnt]!=',')
                                                tmp += line_buffer[cnt++];
                                        //cout<<tmp<<endl;
                                        Signal *s7=C->addSignal(tmp);
                                        n2->addInput(s7);
                                        s7->addOutput(n2);
                                        
                                        tmp="";
                                        cnt++;
                                        cnt++;
                                        while(line_buffer[cnt]!=')')
                                                tmp += line_buffer[cnt++];
                                        //cout<<tmp<<endl;
                                        Signal *s8=C->addSignal(tmp);
                                        n4->addInput(s8);
                                        s8->addOutput(n4);
                                        
                                        //cin.get();                                        
                                }
                                else//OAI22
                                {
                                        Nor2Node *n1=new Nor2Node();
                                        n1->setIndex(index++);
                                        Nor2Node *n2 = new Nor2Node();
                                        n2->setIndex(index++);
                                        Nor2Node *n3=new Nor2Node();
                                        n3->setIndex(index++);
                                        NotNode *n4=new NotNode();
                                        n4->setIndex(index++);
                                        C->addGate(n1);
                                        C->addGate(n2);
                                        C->addGate(n3);
                                        C->addGate(n4);
                                        n4->addOutput(s);
                                        s->addInput(n4);
                                        n4->setIsCrit( true );
                                        //n4->weight = atof(line_buffer2);
                                        
                                        tmp = "s";
                                        char buffer [33];
                                        sprintf (buffer, "%i", C->signalSize());
                                        tmp += buffer;
                                        Signal *s1=C->addSignal(tmp);
                                        n3->addInput(s1);
                                        s1->addOutput(n3);
                                        s1->addInput(n1);
                                        n1->addOutput(s1);
				
                                        tmp = "s";
                                        sprintf (buffer, "%i", C->signalSize());
                                        tmp += buffer;
                                        Signal *s2=C->addSignal(tmp);
                                        n2->addOutput(s2);
                                        s2->addInput(n2);
                                        s2->addOutput(n3);
                                        n3->addInput(s2);
				
                                        tmp = "s";
                                        sprintf (buffer, "%i", C->signalSize());
                                        tmp += buffer;
                                        Signal *s3=C->addSignal(tmp);
                                        n3->addOutput(s3);
                                        s3->addInput(n3);
                                        s3->addOutput(n4);
                                        n4->addInput(s3);
                                        
                                        while(line_buffer[cnt++]!='('){}
                                        tmp="";
                                        while(line_buffer[cnt]!=',')
                                                tmp += line_buffer[cnt++];
                                        //cout<<tmp<<endl;
                                        Signal *s4=C->addSignal(tmp);
                                        n1->addInput(s4);
                                        s4->addOutput(n1);
				
                                        tmp="";
                                        cnt++;
                                        cnt++;
                                        while(line_buffer[cnt]!=',')
                                                tmp += line_buffer[cnt++];
                                        //cout<<tmp<<endl;
                                        Signal *s5=C->addSignal(tmp);
                                        n1->addInput(s5);
                                        s5->addOutput(n1);
                                        
                                        tmp="";
                                        cnt++;
                                        cnt++;
                                        while(line_buffer[cnt]!=',')
                                                tmp += line_buffer[cnt++];
                                        //cout<<tmp<<endl;
                                        Signal *s6=C->addSignal(tmp);
                                        n2->addInput(s6);
                                        s6->addOutput(n2);
                                        
                                        tmp="";
                                        cnt++;
                                        cnt++;
                                        while(line_buffer[cnt]!=')')
                                                tmp += line_buffer[cnt++];
                                        //cout<<tmp<<endl;
                                        Signal *s7=C->addSignal(tmp);
                                        n2->addInput(s7);
                                        s7->addOutput(n2);        
                                        
                                        //cin.get();
                                }
			}
                        else if(tmp=="OAI33"){
                                
                                Nor2Node *n1=new Nor2Node();
                                n1->setIndex(index++);
                                NotNode *n2 = new NotNode();
                                n2->setIndex(index++);
                                Nand2Node *n3=new Nand2Node();
                                n3->setIndex(index++);
                                Nor2Node *n4=new Nor2Node();
                                n4->setIndex(index++);
                                NotNode *n5=new NotNode();
                                n5->setIndex(index++);
                                Nand2Node *n6=new Nand2Node();
                                n6->setIndex(index++);
                                Nand2Node *n7=new Nand2Node();
                                n7->setIndex(index++);
                                
                                C->addGate(n1);
                                C->addGate(n2);
                                C->addGate(n3);
                                C->addGate(n4);
                                C->addGate(n5);
                                C->addGate(n6);
                                C->addGate(n7);
                                n7->addOutput(s);
                                s->addInput(n7);
                                n7->setIsCrit( true );
                                //n7->weight = atof(line_buffer2);
                                
                                tmp = "s";
                                char buffer [33];
                                sprintf (buffer, "%i", C->signalSize());
                                tmp += buffer;
                                Signal *s1=C->addSignal(tmp);
                                n3->addInput(s1);
                                s1->addOutput(n3);
                                s1->addInput(n1);
                                n1->addOutput(s1);
				
                                tmp = "s";
                                sprintf (buffer, "%i", C->signalSize());
                                tmp += buffer;
                                Signal *s2=C->addSignal(tmp);
                                n2->addOutput(s2);
                                s2->addInput(n2);
                                s2->addOutput(n3);
                                n3->addInput(s2);
				
                                tmp = "s";
                                sprintf (buffer, "%i", C->signalSize());
                                tmp += buffer;
                                Signal *s3=C->addSignal(tmp);
                                n3->addOutput(s3);
                                s3->addInput(n3);
                                s3->addOutput(n7);
                                n7->addInput(s3);
                                
                                tmp = "s";
                                sprintf (buffer, "%i", C->signalSize());
                                tmp += buffer;
                                Signal *s4=C->addSignal(tmp);
                                n4->addOutput(s4);
                                s4->addInput(n4);
                                s4->addOutput(n6);
                                n6->addInput(s4);
                                
                                tmp = "s";
                                sprintf (buffer, "%i", C->signalSize());
                                tmp += buffer;
                                Signal *s5=C->addSignal(tmp);
                                n5->addOutput(s5);
                                s5->addInput(n5);
                                s5->addOutput(n6);
                                n6->addInput(s5);
                                
                                tmp = "s";
                                sprintf (buffer, "%i", C->signalSize());
                                tmp += buffer;
                                Signal *s6=C->addSignal(tmp);
                                n6->addOutput(s6);
                                s6->addInput(n6);
                                s6->addOutput(n7);
                                n7->addInput(s6);
                                
                                while(line_buffer[cnt++]!='('){}
                                tmp="";
                                while(line_buffer[cnt]!=',')
                                        tmp += line_buffer[cnt++];
                                //cout<<tmp<<endl;
                                Signal *s7=C->addSignal(tmp);
                                n1->addInput(s7);
                                s7->addOutput(n1);
				
                                tmp="";
                                cnt++;
                                cnt++;
                                while(line_buffer[cnt]!=',')
                                        tmp += line_buffer[cnt++];
                                //cout<<tmp<<endl;
                                Signal *s8=C->addSignal(tmp);
                                n1->addInput(s8);
                                s8->addOutput(n1);
                                        
                                tmp="";
                                cnt++;
                                cnt++;
                                while(line_buffer[cnt]!=',')
                                        tmp += line_buffer[cnt++];
                                //cout<<tmp<<endl;
                                Signal *s9=C->addSignal(tmp);
                                n2->addInput(s9);
                                s9->addOutput(n2);
                                
                                tmp="";
                                cnt++;
                                cnt++;
                                while(line_buffer[cnt]!=',')
                                        tmp += line_buffer[cnt++];
                                //cout<<tmp<<endl;
                                Signal *s10=C->addSignal(tmp);
                                n4->addInput(s10);
                                s10->addOutput(n4);
                                
                                tmp="";
                                cnt++;
                                cnt++;
                                while(line_buffer[cnt]!=',')
                                        tmp += line_buffer[cnt++];
                                //cout<<tmp<<endl;
                                Signal *s11=C->addSignal(tmp);
                                n4->addInput(s11);
                                s11->addOutput(n4);
                                
                                tmp="";
                                cnt++;
                                cnt++;
                                while(line_buffer[cnt]!=')')
                                        tmp += line_buffer[cnt++];
                                //cout<<tmp<<endl;
                                Signal *s12=C->addSignal(tmp);
                                n5->addInput(s12);
                                s12->addOutput(n5);
                                
                                //cin.get();
                               
			}
			else if(tmp=="OR2_X"){
                                Nor2Node *n1=new Nor2Node();
				n1->setIndex(index++);
				NotNode *n2 = new NotNode();
				n2->setIndex(index++);
				C->addGate(n1);
				C->addGate(n2);
                                n2->addOutput(s);
				s->addInput(n2);
                                n2->setIsCrit( true );
                                //n2->weight = atof(line_buffer2);
                                
				tmp = "s";
				char buffer [33];
				sprintf (buffer, "%i", C->signalSize());
				tmp += buffer;
				Signal *s1=C->addSignal(tmp);
				n2->addInput(s1);
				s1->addOutput(n2);
				s1->addInput(n1);
				n1->addOutput(s1);
                                
				while(line_buffer[cnt++]!='('){}
				tmp="";
				while(line_buffer[cnt]!=',')
					tmp += line_buffer[cnt++];
                                //cout<<tmp<<endl;
				Signal *s2=C->addSignal(tmp);
				n1->addInput(s2);
				s2->addOutput(n1);
				
				tmp="";
				cnt++;
				cnt++;
				while(line_buffer[cnt]!=')')
					tmp += line_buffer[cnt++];
                                //cout<<tmp<<endl;
				Signal *s3=C->addSignal(tmp);
				n1->addInput(s3);
				s3->addOutput(n1);
                                
                                //cin.get();
                               
			}
			else if(tmp=="OR3_X"){
                                Nor2Node *n1=new Nor2Node();
				n1->setIndex(index++);
				NotNode *n2 = new NotNode();
				n2->setIndex(index++);
				Nand2Node *n3=new Nand2Node();
				n3->setIndex(index++);
				C->addGate(n1);
				C->addGate(n2);
				C->addGate(n3);
				n3->addOutput(s);
				s->addInput(n3);
                                n3->setIsCrit( true );
                                //n3->weight = atof(line_buffer2);
                                
				tmp = "s";
				char buffer [33];
				sprintf (buffer, "%i", C->signalSize());
				tmp += buffer;
				Signal *s1=C->addSignal(tmp);
				n3->addInput(s1);
				s1->addOutput(n3);
				s1->addInput(n1);
				n1->addOutput(s1);
				
                                tmp = "s";
                                sprintf (buffer, "%i", C->signalSize());
				tmp += buffer;
				Signal *s2=C->addSignal(tmp);
                                n3->addInput(s2);
				s2->addOutput(n3);
				s2->addInput(n2);
				n2->addOutput(s2);
				
				while(line_buffer[cnt++]!='('){}
				tmp="";
				while(line_buffer[cnt]!=',')
					tmp += line_buffer[cnt++];
                                //cout<<tmp<<endl;
				Signal *s3=C->addSignal(tmp);
				n1->addInput(s3);
				s3->addOutput(n1);
				
				tmp="";
				cnt++;
				cnt++;
				while(line_buffer[cnt]!=',')
					tmp += line_buffer[cnt++];
                                //cout<<tmp<<endl;
				Signal *s4=C->addSignal(tmp);
				n1->addInput(s4);
				s4->addOutput(n1);
				
                                tmp="";
				cnt++;
				cnt++;
				while(line_buffer[cnt]!=')')
					tmp += line_buffer[cnt++];
                                //cout<<tmp<<endl;
				Signal *s5=C->addSignal(tmp);
				n2->addInput(s5);
				s5->addOutput(n2);
                                
                                //cin.get();
			}
                        else if(tmp=="OR4_X"){
                                Nor2Node *n1=new Nor2Node();
				n1->setIndex(index++);
				Nor2Node *n2 = new Nor2Node();
				n2->setIndex(index++);
				Nand2Node *n3=new Nand2Node();
				n3->setIndex(index++);
				C->addGate(n1);
				C->addGate(n2);
				C->addGate(n3);
				n3->addOutput(s);
				s->addInput(n3);
                                n3->setIsCrit( true );
                                //n3->weight = atof(line_buffer2);
                                
				tmp = "s";
				char buffer [33];
				sprintf (buffer, "%i", C->signalSize());
				tmp += buffer;
				Signal *s1=C->addSignal(tmp);
				n3->addInput(s1);
				s1->addOutput(n3);
				s1->addInput(n1);
				n1->addOutput(s1);
				
                                tmp = "s";
                                sprintf (buffer, "%i", C->signalSize());
				tmp += buffer;
				Signal *s2=C->addSignal(tmp);
                                n3->addInput(s2);
				s2->addOutput(n3);
				s2->addInput(n2);
				n2->addOutput(s2);
				
				while(line_buffer[cnt++]!='('){}
				tmp="";
				while(line_buffer[cnt]!=',')
					tmp += line_buffer[cnt++];
                                //cout<<tmp<<endl;
				Signal *s3=C->addSignal(tmp);
				n1->addInput(s3);
				s3->addOutput(n1);
				
				tmp="";
				cnt++;
				cnt++;
				while(line_buffer[cnt]!=',')
					tmp += line_buffer[cnt++];
                                //cout<<tmp<<endl;
				Signal *s4=C->addSignal(tmp);
				n1->addInput(s4);
				s4->addOutput(n1);
				
                                tmp="";
				cnt++;
				cnt++;
				while(line_buffer[cnt]!=',')
					tmp += line_buffer[cnt++];
                                //cout<<tmp<<endl;
				Signal *s5=C->addSignal(tmp);
				n2->addInput(s5);
				s5->addOutput(n2);
				
				tmp="";
				cnt++;
				cnt++;
				while(line_buffer[cnt]!=')')
					tmp += line_buffer[cnt++];
                                //cout<<tmp<<endl;
				Signal *s6=C->addSignal(tmp);
				n2->addInput(s6);
				s6->addOutput(n2);
				
                                //cin.get();
                                
			}
			else if(tmp=="XNOR2"){
				Nand2Node *n1=new Nand2Node();
				n1->setIndex(index++);
				Nor2Node *n2 = new Nor2Node();
				n2->setIndex(index++);
				NotNode *n3=new NotNode();
				n3->setIndex(index++);
                                Nand2Node *n4=new Nand2Node();
				n4->setIndex(index++);
				C->addGate(n1);
				C->addGate(n2);
				C->addGate(n3);
                                C->addGate(n4);
				n4->addOutput(s);
				s->addInput(n4);
                                n4->setIsCrit( true );
				//n4->weight = atof(line_buffer2);
                                
				tmp = "s";
				char buffer [33];
				sprintf (buffer, "%i", C->signalSize());
				tmp += buffer;
				Signal *s1=C->addSignal(tmp);
				n4->addInput(s1);
				s1->addOutput(n4);
                                s1->addInput(n1);
				n1->addOutput(s1);
				
				tmp = "s";
				sprintf (buffer, "%i", C->signalSize());
				tmp += buffer;
				Signal *s2=C->addSignal(tmp);
				n2->addOutput(s2);
				s2->addInput(n2);
				s2->addOutput(n3);
				n3->addInput(s2);
				
                                tmp = "s";
				sprintf (buffer, "%i", C->signalSize());
				tmp += buffer;
				Signal *s3=C->addSignal(tmp);
				n3->addOutput(s3);
				s3->addInput(n3);
				s3->addOutput(n4);
				n4->addInput(s3);
				
				while(line_buffer[cnt++]!='('){}
				tmp="";
				while(line_buffer[cnt]!=',')
					tmp += line_buffer[cnt++];
                                //cout<<tmp<<endl;
				Signal *s4=C->addSignal(tmp);
				n1->addInput(s4);
				s4->addOutput(n1);
				n2->addInput(s4);
				s4->addOutput(n2);
				
				tmp="";
				cnt++;
				cnt++;
				while(line_buffer[cnt]!=')')
					tmp += line_buffer[cnt++];
                                //cout<<tmp<<endl;
				Signal *s5=C->addSignal(tmp);
				n1->addInput(s5);
				s5->addOutput(n1);
				n2->addInput(s5);
				s5->addOutput(n2);
				
                                //cin.get();
			}
			else if(tmp=="XOR2_"){
                                Nand2Node *n1=new Nand2Node();
				n1->setIndex(index++);
				Nand2Node *n2 = new Nand2Node();
				n2->setIndex(index++);
				Nand2Node *n3=new Nand2Node();
				n3->setIndex(index++);
                                Nand2Node *n4=new Nand2Node();
				n4->setIndex(index++);
				C->addGate(n1);
				C->addGate(n2);
				C->addGate(n3);
                                C->addGate(n4);
				n4->addOutput(s);
				s->addInput(n4);
                                n4->setIsCrit( true );
				//n4->weight = atof(line_buffer2);
                                
				tmp = "s";
				char buffer [33];
				sprintf (buffer, "%i", C->signalSize());
				tmp += buffer;
				Signal *s1=C->addSignal(tmp);
				n2->addInput(s1);
				s1->addOutput(n2);
                                n3->addInput(s1);
				s1->addOutput(n3);
				s1->addInput(n1);
				n1->addOutput(s1);
				
				tmp = "s";
				sprintf (buffer, "%i", C->signalSize());
				tmp += buffer;
				Signal *s2=C->addSignal(tmp);
				n2->addOutput(s2);
				s2->addInput(n2);
				s2->addOutput(n4);
				n4->addInput(s2);
				
                                tmp = "s";
				sprintf (buffer, "%i", C->signalSize());
				tmp += buffer;
				Signal *s3=C->addSignal(tmp);
				n3->addOutput(s3);
				s3->addInput(n3);
				s3->addOutput(n4);
				n4->addInput(s3);
				
				while(line_buffer[cnt++]!='('){}
				tmp="";
				while(line_buffer[cnt]!=',')
					tmp += line_buffer[cnt++];
                                //cout<<tmp<<endl;
				Signal *s4=C->addSignal(tmp);
				n1->addInput(s4);
				s4->addOutput(n1);
				n2->addInput(s4);
				s4->addOutput(n2);
                                
				tmp="";
				cnt++;
				cnt++;
				while(line_buffer[cnt]!=')')
					tmp += line_buffer[cnt++];
                                //cout<<tmp<<endl;
				Signal *s5=C->addSignal(tmp);
				n1->addInput(s5);
				s5->addOutput(n1);
				n3->addInput(s5);
				s5->addOutput(n3);
				
                                //cin.get();
                                
			}
			else{
				cout<<line_num<<" has error! "<<line_buffer<<endl;
				exit(1);
			}
		}
	}
        /*
        ofstream out("ck");
        for(int i = 0; i < C->gateSize(); i++)
                if(C->getGate(i)->outputSize() != 0)
                        out << C->getGate(i)->getOutput(0)->getName() << "\t" << C->getGate(i)->weight << endl;
                else
                        out << C->getGate(i)->weight << endl;
        
        out.close();
        */
        cout<<line_num<<" Reading Inst ends here "<<line_buffer<<endl;
	cout<<"## No of FlipFlops:"<<C->flipflopSize()<<endl<<endl;;
	cout<<"## No of Gates:"<<C->gateSize()<<endl<<endl;;
        
	if(VERBOSE){
		for(int i=0; i<C->inputSize(); ++i)
			//cout<<C->getInput(i)->str()<<endl;
		for(int i=0; i<C->outputSize(); ++i)
			//cout<<C->getOutput(i)->str()<<endl;
		for(int i=0; i<C->flipflopSize(); ++i)
			//cout<<C->getFlipflop(i)->str()<<endl;
		for(int i=0; i<C->gateSize(); ++i)
			//cout<<C->getGate(i)->str()<<endl;
		//cout<<"#of Signals: "<<C->signalSize()<<endl;
		
		for(int i=0; i<C->signalSize(); ++i){
			Signal *s=C->getSignal(i);
			//cout<<s->str()<<endl;
			////cin.get();
                        //	//cout<<"  "<<((s->getOut())? s->getOut()->str():s->getIn()->str())<<endl;
		}
		
/*		for(int i=0; i<C->signalSize(); ++i){
                //cout<<C->getSignal(i)->getName()<<":";
                switch (C->getSignal(i)->getOut()->signature()){
                case OUTPUT:
                //cout<<"OUTPUT";
                break;
                case INPUT:
                //cout<<"INPUT";
                break;
                default:
                //cout<<"error";
                }
                }*/
        }
	
	return true;
}

